import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:juming_test/model/domain/domain_filter_option_model.dart';

/// Checkbox组件
class DomainFilterCheckbox extends StatelessWidget {
  const DomainFilterCheckbox({
    super.key,
    required this.list,
    required this.onChanged,
  });

  // 数据源
  final List<DomainFilterOptionModel> list;
  // 选中回调
  final Function(List<DomainFilterOptionModel>) onChanged;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(top: 8.w),
      child: Wrap(
        children:
            list.map((item) {
              return GestureDetector(
                onTap: () {
                  onItemTap(item, list);
                },
                child: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Container(
                      width: 20.w,
                      height: 20.w,
                      decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        border: Border.all(
                          color:
                              item.isSelected
                                  ? Colors.transparent
                                  : Colors.grey,
                          width: 1.w,
                        ),
                        color:
                            item.isSelected
                                ? Colors.red[400]
                                : Colors.transparent,
                      ),
                      child:
                          item.isSelected
                              ? Icon(
                                Icons.check,
                                size: 16.w,
                                color: Colors.white,
                              )
                              : null,
                    ),
                    SizedBox(width: 6.w),
                    Text(item.name),
                    SizedBox(width: 18.w),
                  ],
                ),
              );
            }).toList(),
      ),
    );
  }

  // 点击事件
  void onItemTap(
    DomainFilterOptionModel item,
    List<DomainFilterOptionModel> list,
  ) {
    List<DomainFilterOptionModel> newSelectedValues =
        list.where((element) => element.isSelected).toList();
    if (newSelectedValues.contains(item)) {
      newSelectedValues.remove(item);
    } else {
      newSelectedValues.add(item);
    }
    onChanged(newSelectedValues);
  }
}
